import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

//添加以下代码
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('@/projects/threeJs/views/plp/test.vue'),
      meta: {title: '通用界面加载'}
    },
    {
      path: '/kb',
      name: 'kb',
      component: () => import('@/projects/threeJs/views/kb/kb.vue'),
      meta: {title: '通用界面加载'}
    },
    {
      path: '/demo',
      name: 'demo',
      component: () => import('@/projects/threeJs/views/kb/demo.vue'),
      meta: {title: '通用界面加载'}
    },
    // {
    //   path: '/demo2',
    //   name: 'demo2',
    //   component: () => import('@/projects/threeJs/views/plp/test2.vue'),
    //   meta: {title: '通用界面加载'}
    // },
  ]

})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
  let token = to.query.token
  if (token) {
    let localToken = localStorage.getItem('token')
    //判断本地是否有token或则本地token和新token不一致则更新token
    if (!localToken || token != localToken) {
      localStorage.setItem('token', token)
    }
  }
  next()
})

export default router
